package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.Dept;
import com.woniu.entity.ProPurchase;
import com.woniu.entity.PurApprove;
import com.woniu.entity.User;
import com.woniu.mapper.PurApproveMapper;
import com.woniu.service.IProPurApproveService;
import com.woniu.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *<p>
 *采购审批的实现类
 *</p>
 * @author ZhouWu
 * @date 2022/8/30 16:37
 */

@Service
@Transactional
//@SuppressWarnings("all")
public class PurApproveServiceImpl extends ServiceImpl<PurApproveMapper, PurApprove> implements IProPurApproveService {
    @Autowired
    private PurApproveMapper purApproveMapper;

    @Override
    public void add(PurApprove purApprove) {
        purApproveMapper.add(purApprove);
    }

    @Override
    public void delete(int purApproveId) {
        purApproveMapper.delete(purApproveId);
    }

    @Override
    public void update(PurApprove purApprove) {
        purApproveMapper.update(purApprove);
    }

    @Override
    public List<PurApprove> getAll() {
        List<PurApprove> purApproves = new ArrayList<>();
        purApproves = purApproveMapper.getall();
        return  purApproves;
    }

    @Override
    public PurApprove getByPurApproveId(int purApproveId) {
        return purApproveMapper.getByPurApproveId(purApproveId);
    }

    @Override
    public List<PurApprove> getByDeptId(int deptId) {
        return purApproveMapper.getByDeptId(deptId);
    }

    @Override
    public List<PurApprove> getByUserId(int userId) {
        return purApproveMapper.getByUserId(userId);
    }

    @Override
    public List<PurApprove> getRefuseByPurUserId(int userId) {
        return purApproveMapper.getRefuseByPurUserId(userId);
    }

    @Override
    public void pass(int purApproveId, String apdate, String apfeedback) {
        purApproveMapper.pass(purApproveId,apdate,apfeedback);
    }

    @Override
    public void refuse(int purApproveId, String apdate, String apfeedback) {
        purApproveMapper.refuse(purApproveId,apdate,apfeedback);
    }

    @Override
    public Page<PurApprove> getPurApproveByPage(Integer pageNum, Integer pageSize, PurApprove purApprove) {
        QueryWrapper wrapper = getApproveWrapper(purApprove);
        Page<PurApprove> page = new Page<>(pageNum,pageSize);
        return purApproveMapper.getPurApproveByPage(page,wrapper);
    }

    @Override
    public List<PurApprove> getPurApproveByCondition(PurApprove purApprove) {
        List<PurApprove> purApproves = new ArrayList<>();
        QueryWrapper wrapper = getApproveWrapper(purApprove);
        purApproves = purApproveMapper.getPurApproveByCondition(wrapper);
        return purApproves;
    }

    private static QueryWrapper getApproveWrapper(PurApprove purApprove) {
        QueryWrapper<PurApprove> wrapper = new QueryWrapper<>();
        if (purApprove == null){
            wrapper.eq("1","1");
            return wrapper;
        };
        if (EmptyUtil.isNoEmpty(purApprove.getPurApproveId())) {
            wrapper.eq("pur_approve_id", purApprove.getPurApproveId());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getProPurchase())) {
            wrapper.eq("pur_id", purApprove.getProPurchase().getPurId());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getDept()) ) {
            wrapper.eq("dept_id", purApprove.getDept().getDeptId());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getUser()) ) {
            wrapper.eq("user_id", purApprove.getUser().getUserId());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getPurApplyDate()) ) {
            wrapper.eq("pur_apply_date", purApprove.getPurApplyDate());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getPurApproveDate())) {
            wrapper.eq("pur_approve_date", purApprove.getPurApproveDate());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getPurApproveFeedback())) {
            wrapper.eq("pur_approve_feedback", purApprove.getPurApproveFeedback());
        };
        if (EmptyUtil.isNoEmpty(purApprove.getPurApproveStatus()) ) {
            wrapper.eq("pur_approve_status", purApprove.getPurApproveStatus());
        };
        return wrapper;
    }

}
